package com.wss.module.main.ui.view;

import com.wss.module.main.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * Describe： 模拟iOS风格开关按钮
 * Created by 吴天强 on 2020/9/16.
 */
public class MarqueeView extends ScrollView implements Runnable {

    private Context context;
    private DirectionalLayout mainLayout;//跑马灯滚动部分
    private int scrollSpeed = 5;//滚动速度
    private int scrollDirection = LEFT_TO_RIGHT;//滚动方向
    private int currentX;//当前x坐标
    private int viewMargin = 20;//View间距
    private int viewWidth;//View总宽度
    private int screenWidth;//屏幕宽度

    public static final int LEFT_TO_RIGHT = 1;
    public static final int RIGHT_TO_LEFT = 2;

    public MarqueeView(Context context) {
        this(context, null);
    }

    public MarqueeView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public MarqueeView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        initView();
    }

    void initView() {
        DisplayAttributes attributes = DisplayManager.getInstance()
                .getDefaultDisplay(getContext())
                .get()
                .getRealAttributes();
        screenWidth = attributes.width;
        mainLayout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_scroll_content, null, false);
        addComponent(mainLayout);
    }

    public void addViewInQueue(Component view) {
        DirectionalLayout.LayoutConfig lc = new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_CONTENT,
                LayoutConfig.MATCH_CONTENT
        );
        lc.setMargins(viewMargin, 0, 0, 0);
        view.setLayoutConfig(lc);
        mainLayout.addComponent(view);
        view.estimateSize(0, 0);
        viewWidth = viewWidth + view.getEstimatedWidth() + viewMargin;
    }

    // TODO 开始滚动
    public void startScroll() {
//        removeCallbacks(this);
        currentX = (scrollDirection == LEFT_TO_RIGHT ? viewWidth : -screenWidth);
//        post(this);
    }

    //TODO 停止滚动
    public void stopScroll() {
//        removeCallbacks(this);
    }

    //设置View间距
    public void setViewMargin(int viewMargin) {
        this.viewMargin = viewMargin;
    }

    //设置滚动速度
    public void setScrollSpeed(int scrollSpeed) {
        this.scrollSpeed = scrollSpeed;
    }

    //设置滚动方向 默认从左向右
    public void setScrollDirection(int scrollDirection) {
        this.scrollDirection = scrollDirection;
    }

    @Override
    public void run() {
        switch (scrollDirection) {
            case LEFT_TO_RIGHT:
                mainLayout.scrollTo(currentX, 0);
                currentX--;

                if (-currentX >= screenWidth) {
                    mainLayout.scrollTo(viewWidth, 0);
                    currentX = viewWidth;
                }
                break;
            case RIGHT_TO_LEFT:
                mainLayout.scrollTo(currentX, 0);
                currentX++;

                if (currentX >= viewWidth) {
                    mainLayout.scrollTo(-screenWidth, 0);
                    currentX = -screenWidth;
                }
                break;
            default:
                break;
        }
        //TODO 更新view
//        postDelayed(this, 50 / scrollSpeed);
    }

}
